package com.zhangtianyi.netty.demo_4.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

public class MyServerHandler extends ChannelInboundHandlerAdapter {//继承适配器是SimpleChannelInboundHandler父类

    /**
     * 事件触发（调用此方法）会传给pipline中的下一个handler
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof IdleStateEvent){
            IdleStateEvent event = (IdleStateEvent) evt;

            String eventType = null;

            switch (event.state()){
                case READER_IDLE:
                    eventType = "读空闲...";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲...";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
            }

            System.out.println(ctx.channel().remoteAddress()+"超时事件:"+eventType);

            ctx.channel().close();

        }
    }
}
